<?php
/**
 * @File	  : base_model.php
 * @Project	  : vncphathoc
 * @author	  : ThanhMinh
 * @email	  : thanhminhld@yahoo.com - thanhminhld@gmail.com
 * @copyright : 2009
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Base_Model extends Model
{
	protected $_tbl = null;
	/**
	 * constructor function
	 */
	public function __construct()
	{
		parent::Model();
	}
    /**
     * @function
     *
     * @author Vo Thanh Minh <vothanhminh@kng-vn.com>
     * @access public
     * @param
     * @return void
     */
    public function optimize_Tbl()
    {
        $this->db->optimize_tbl($this->_tbl);
    }
	/**
	 * get_row
	 * @param array $where
	 * @return $result object
	 */
    public function get_row($where = null)
    {
        $result = null;
        $query = $this->db->get_where($this->_tbl, $where);
        if ($query->num_rows() > 0) {
            $result = $query->row_object();
        }
        return $result;
    }
	/**
	 * getTotalRows
	 *
	 * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access public
	 * @param  array
	 * @param  string
	 * @return int
	 */
    public function getTotalRows($arrWhere = array(), $field_groupby = '')
    {
	    if (count($arrWhere) > 0) {
            $this->db->where($arrWhere);
        }
        if (!empty($field_groupby) && is_string($field_groupby)) {
            if ($this->db->field_exists($field_groupby, $this->_tbl)) {
                $this->db->group_by($field_groupby);
            }
        }
		$query = $this->db->get_where($this->_tbl, $arrWhere);

		return $query->num_rows();
    }
	/**
	 * @function loadList
	 *
	 * @author 	 Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access 	 public
	 * @param	 array
	 * @param	 int
	 * @param	 int
	 * @param	 string
	 * @param	 string
	 * @return 	 array
	 */
	public function loadList($arrWhere = array(), $limit = 10, $offset = 0, $order = '', $mode = '')
	{
	    if (is_string($order) && strlen($order) > 0) {
	        if ( ! preg_match('/(asc|ASC|desc|DESC)/', $mode)) {
                $mode = 'asc';
	        }

	        $this->db->order_by($order, $mode);
	    }

        if ($limit == 'all') {
            $query = $this->db->get_where($this->_tbl, $arrWhere);

        } else {
            $query = $this->db->get_where($this->_tbl, $arrWhere, $limit, $offset);
        }
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList[] = $row;
            }
        }
		return $arrList;
	}
	/**
	 * loadListWhereIn
	 *
	 * @access	: public
	 * @author	: Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @param	: string $field_sel
	 * @param	: sring $fieldWhereIn
	 * @param	: string or array $arrWhereIn
	 *
	 * @return	: NULL or FALSE or array
	 */
	public function loadListWhereIn($field_sel, $fieldWhereIn = '', $arrWhereIn = '', $otherConditionWhere = array(),
									$order = '', $mode = '', $limit = 10, $offset = 0)
	{
	    $strField = '';
	    $this->db->distinct();
	    if (is_array($field_sel) && count($field_sel) > 0) {
	        $strField = implode(',', $field_sel);
	        $this->db->select($strField);
	    } else if (is_string($strField) && strlen($strField) > 0) {
	        $this->db->select($strField);
	    }
	    if (!is_string($fieldWhereIn) || strlen($fieldWhereIn) <= 0) {
	        return FALSE;
	    }
	    if (is_array($arrWhereIn) && count($arrWhereIn) > 0) {
	        $this->db->where_in($fieldWhereIn, $arrWhereIn);

	    } else if (is_string($arrWhereIn) && strlen($arrWhereIn) > 0) {
	        $this->db->where($fieldWhereIn, $arrWhereIn);
	    } else {
	        return FALSE;
	    }
	    if (is_array($otherConditionWhere) && count($otherConditionWhere) > 0) {
	        $this->db->where($otherConditionWhere);
	    }
		if (is_string($order) && strlen($order) > 0) {
	        if ( ! preg_match('/(asc|ASC|desc|DESC)/', $mode)) {
                $mode = 'asc';
	        }

	        $this->db->order_by($order, $mode);
	    }
	    $query = $this->db->get($this->_tbl, $limit, $offset);

        if (!is_object($query)) {
            return NULL;
        }
        $arrist = array();
		if ($query->num_rows() > 0) {
			foreach($query->result() as $row) {

				$arrist[] = $row;
			}
		}
        return $arrist;
	}
}
?>